#!/bin/bash
#----------------------------------------------------------------------------
# Purpose:
# Copyright Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
#----------------------------------------------------------------------------
set -e
TOP_DIR=$(pwd)
CMD=""
OFED_VER=""
KERNEL_VER="$(uname -r)"
KERNEL_VER_PATH="/usr/lib/modules"

help() {
    echo -e "\nA scripts to compile driver of nic or roce, all parameters are the following:\n"
    echo -e "-d      driver to be installed(nic or roce)"
    echo -e "-o      version of ofed(M5_4 or M5_8)"
    echo -e "-k      version of kernel, which is in /usr/lib/modules/"
    echo -e "Other cases show the help information of this script\n"
    exit 0
}

change_Makefile()
{
    sed -i "s/KERNEL_VER.*:=.*/KERNEL_VER:=${KERNEL_VER}/g" ./Makefile
}

change_spec()
{
    for file in ./*
    do
        num=$(grep -n "%define ker_ver" $file |awk -F ':' '{print $1}')
        sed -i "${num}s/uname -r/echo %(uname -r)/" $file
    done

    sed -i "s/%(uname -r)/${KERNEL_VER}/g" ./*.spec
}

build_udk()
{
    cd ${TOP_DIR}/udk/kernel
    change_Makefile
    make clean
    make -j 16
    cp hiudk3.ko ${TOP_DIR}/output
    cp Module.symvers ${TOP_DIR}/output/UDK_Module.symvers
}

build_sdk()
{
    cd ${TOP_DIR}/hwsdk
    change_Makefile
    make clean
    make -j 16
    cp hisdk3.ko ${TOP_DIR}/output
    cp Module.symvers ${TOP_DIR}/output/SDK_Module.symvers
}

build_hinic()
{
    cd ${TOP_DIR}/hinic
    change_Makefile
    make clean
    make -j 16
    cp hinic3.ko ${TOP_DIR}/output
    cp Module.symvers ${TOP_DIR}/output/NIC_Module.symvers
}

build_hiroce_kern()
{
    cd ${TOP_DIR}/roce/linux/kernel
    change_Makefile
    make clean
    make OFED_VERSION="$1" -j 16
    cp hiroce3.ko ${TOP_DIR}/output
}

build_hiroce_user()
{
    cd ${TOP_DIR}/roce/linux/user
    cp -rf ${TOP_DIR}/roce/linux/user/libh*.so ${TOP_DIR}/output
}

build_rpm()
{
    cd ${TOP_DIR}
    mkdir -pv -m 777 rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
    cp output/*.ko rpmbuild/SOURCES
    if [ "$CMD" == "roce" ]; then
        cp output/*.so rpmbuild/SOURCES
    fi
    cp GLOBAL_VERSION_NEW rpmbuild/SOURCES
    cp conf/* rpmbuild/SOURCES
    cp specs/*.spec rpmbuild/SPECS
    cd rpmbuild/SPECS
    change_spec
    if [[ $(uname -p) = "x86_64" ]]; then
        rpmbuild --define "_topdir ${TOP_DIR}/rpmbuild" -bb hisdk3_rhel.spec
        rpmbuild --define "_topdir ${TOP_DIR}/rpmbuild" -bb hinic3_rhel.spec
        if [ "$CMD" == "roce" ]; then
              rpmbuild --define "_topdir ${TOP_DIR}/rpmbuild" -bb hiroce3-compute_rhel.spec
        fi
        cp -f ${TOP_DIR}/rpmbuild/RPMS/x86_64/*.rpm ${TOP_DIR}/output/
    else
        rpmbuild --define "_topdir ${TOP_DIR}/rpmbuild" -bb hisdk3_rhel_arm.spec --target=aarch64
        rpmbuild --define "_topdir ${TOP_DIR}/rpmbuild" -bb hinic3_rhel_arm.spec --target=aarch64
        if [ "$CMD" == "roce" ]; then
            rpmbuild --define "_topdir ${TOP_DIR}/rpmbuild" -bb hiroce3-compute_rhel.spec --target=aarch64
        fi
    cp -f ${TOP_DIR}/rpmbuild/RPMS/aarch64/*.rpm ${TOP_DIR}/output/
    fi
}

install_rpm()
{
    cd ${TOP_DIR}/output
    rpm -Uvh *hisdk3*.rpm --force --nodeps
    rpm -Uvh *hinic3*.rpm --force --nodeps
    if [ "$CMD" == "roce" ]; then
        rpm -Uvh *hiroce3*.rpm --force --nodeps
    fi
}

main()
{
    while getopts ":d:o:k:" opt
    do
        case $opt in
            d)
            if [ -n "$OPTARG" ]; then
                if [ "$OPTARG" == "nic" ]; then
                    CMD="nic"
                elif [ "$OPTARG" == "roce" ]; then
                    CMD="roce"
                else
                    echo "Unsupport this command."
                    return 1
                fi
            fi
            ;;
            o)
            if [ -n "$OPTARG" ]; then
                if [ "$OPTARG" == "M5_8" ]; then
                    OFED_VER="OFED_MLNX_5_7_8"
                elif [ "$OPTARG" == "M5_4" ]; then
                    OFED_VER="OFED_MLNX_5_4"
                else
                    echo "Unsupport this ofed."
                    return 1
                fi
            fi
            ;;
            k)
            if [ -n "$OPTARG" ]; then
                if [ -d "${KERNEL_VER_PATH}/${OPTARG}/" ];then 
                    KERNEL_VER=$OPTARG
                    echo "The kernel version is $OPTARG."
                else
                    echo "The kernel version is not exist."
                    return 1
                fi
            fi
            ;;
            *)
            echo "Unsupport this option."
            help
            return 1
            ;;
        esac
    done

    cd ${TOP_DIR}
    [ -d "output" ] && rm -rf output
    mkdir -p output

    [ -d "rpmbuild" ] && rm -rf rpmbuild

    build_udk
    build_sdk
    build_hinic
    if [ "$CMD" == "roce" ]; then
        if [ -z "$OFED_VER" ]; then
            echo "Please input version of ofed."
            return 1
        else
            build_hiroce_kern $OFED_VER
            build_hiroce_user
        fi
    fi
    build_rpm
    install_rpm
}

main $@
